﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Text.RegularExpressions;
using System.Data.SqlClient;

using CE.Global;
using CE.Image;
using CE.CreditCard;
using CE.Database;
using CE.Configuration;

public partial class Admin_credit_card_UpdateCardImage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/Admin/AdminSecureLogin.aspx");
        }
       if (!IsPostBack)
        {
            clearProcess();
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if ((Uploadcardimage.FileName != null) && (Uploadcardimage.FileName != ""))
        {
            if (Page.IsValid)
            {
                CCImage _ccimage = new CCImage();
                CCImageDB _ccimagedb = new CCImageDB();

                _ccimage.CCId = Convert.ToInt64(lblCCId.Text);

                HttpPostedFile myFile = Uploadcardimage.PostedFile;
                int fileSize = myFile.ContentLength;
                MyImage myImage = new MyImage();
                byte[] myData = new byte[fileSize];
                myFile.InputStream.Read(myData, 0, fileSize);
                string strFilename = Path.GetFileName(myFile.FileName);
                string strUploadImagePath = CE.Configuration.MyConfigurationManager.GetAppSettingValue("UploadCardImagePath");
                string uploadPath = Server.MapPath(strUploadImagePath + strFilename);

                _ccimage.CreditcardImage = strUploadImagePath + strFilename;
                if (File.Exists(uploadPath))
                {
                }
                else
                {
                    myImage.WriteToFile(uploadPath, ref myData);
                }
                _ccimage.Status = true;
                _ccimage.CCId = _ccimagedb.Update(_ccimage);

                if (_ccimage.CCId > 0)
                {
                    clearProcess();
                    lblMessage.Text = "Saved successfully!";
                }
                else
                {
                    lblError.Text = "Saving failed!";
                }
            }
        }
        else
        {
            lblError.Text = "Select Card Image";
        }
    }

    protected void ddlCardclass_SelectedIndexChanged(object sender, EventArgs e)
    {
        lblError.Text = "";
        lblMessage.Text = "";
        Int64 _cardId = -1;
        _cardId = Convert.ToInt64(ddlCardclass.SelectedValue);
        if (_cardId != -1)
        {
            DataTable _dtProductName = General.GetComboItem("ProductName", "CCClass", _cardId.ToString(), false);
            ddlProductName.DataSource = _dtProductName;
            ddlProductName.DataTextField = _dtProductName.Columns[0].ColumnName.ToString();
            ddlProductName.DataValueField = _dtProductName.Columns[1].ColumnName.ToString();
            ddlProductName.DataBind();
            ddlProductName.Items.Insert(0, new ListItem("Select", "-1"));
            imgCardLogo.ImageUrl = "~/captcha/temp_card.gif";
            lblServerUrl.Text = "";
            
        }
        else
        {
            lblCCId.Text = "";
            ddlProductName.Items.Clear();
            ddlProductName.Items.Insert(0, new ListItem("Select", "-1"));
            imgCardLogo.ImageUrl = "~/captcha/temp_card.gif";
            lblServerUrl.Text = "";
        }
        ddlProviderCompanyName.Items.Clear();
        DataTable _dtCompany = General.GetComboItem("ProviderCompany", false);
        ddlProviderCompanyName.DataSource = _dtCompany;
        ddlProviderCompanyName.DataTextField = _dtCompany.Columns[0].ColumnName.ToString();
        ddlProviderCompanyName.DataValueField = _dtCompany.Columns[1].ColumnName.ToString();
        ddlProviderCompanyName.DataBind();
        ddlProviderCompanyName.Items.Insert(0, new ListItem("Select", "-1"));
    }

    protected void ddlProviderCompanyName_SelectedIndexChanged(object sender, EventArgs e)
    {
        Int64 _cardId = -1;
        Int32 _financeId = -1;
        _cardId = Convert.ToInt64(ddlCardclass.SelectedValue);
        _financeId = Convert.ToInt32(ddlProviderCompanyName.SelectedValue);
        if ((_cardId != -1) && (_financeId != -1))
        {
            lblCCId.Text = "";
            ddlProductName.Items.Clear();
            DataTable _dtProductName = GetProductName("ProductName", "CCClass", _cardId, "FinancialInstitute", _financeId);
            ddlProductName.DataSource = _dtProductName;
            ddlProductName.DataTextField = _dtProductName.Columns[0].ColumnName.ToString();
            ddlProductName.DataValueField = _dtProductName.Columns[1].ColumnName.ToString();
            ddlProductName.DataBind();
            ddlProductName.Items.Insert(0, new ListItem("Select", "-1"));
            imgCardLogo.ImageUrl = "~/captcha/temp_card.gif";
            lblServerUrl.Text = "";

        }
        else if (_cardId != -1)
        {
            lblCCId.Text = "";
            ddlProductName.Items.Clear();
            DataTable _dtProductName = General.GetComboItem("ProductName", "CCClass", _cardId.ToString(), false);
            ddlProductName.DataSource = _dtProductName;
            ddlProductName.DataTextField = _dtProductName.Columns[0].ColumnName.ToString();
            ddlProductName.DataValueField = _dtProductName.Columns[1].ColumnName.ToString();
            ddlProductName.DataBind();
            ddlProductName.Items.Insert(0, new ListItem("Select", "-1"));
            imgCardLogo.ImageUrl = "~/captcha/temp_card.gif";
            lblServerUrl.Text = "";
        }
        else
        {
            lblCCId.Text = "";
            ddlProductName.Items.Clear();
            ddlProductName.Items.Insert(0, new ListItem("Select", "-1"));
            imgCardLogo.ImageUrl = "~/captcha/temp_card.gif";
            lblServerUrl.Text = "";
        }
    }

    protected void ddlProductName_SelectedIndexChanged(object sender, EventArgs e)
    {
        CCImage _ccimage = new CCImage();
        CCImageDB _ccimagedb = new CCImageDB();
        _ccimage = _ccimagedb.GetCCImage(Convert.ToInt64(ddlProductName.SelectedValue));
        lblCCId.Text = ddlProductName.SelectedValue;
        if (_ccimage != null)
        {
            if (_ccimage.CreditcardImage != String.Empty)
            {
                lblServerUrl.Text = _ccimage.CreditcardImage;
                imgCardLogo.ImageUrl = _ccimage.CreditcardImage;
            }
            else
            {
                imgCardLogo.ImageUrl = "~/captcha/temp_card.gif";
                lblServerUrl.Text = "";
            }

        }
        else
        {
            imgCardLogo.ImageUrl = "~/captcha/temp_card.gif";
            lblServerUrl.Text = "";
        }
    }
    
    public static DataTable GetProductName(string keyName, string classkey, Int64 classId, string financekey, Int32 financeId)
    {
        DataTable _dt = null;
        try
        {
            MyDatabase _db = new MyDatabase();

            string _keyValue = MyConfigurationManager.GetAppSettingValue(keyName);
            string[] _keyValueList = _keyValue.Split(',');

            string _sqlstatement = "SELECT " + _keyValueList[2] + ", " + _keyValueList[1] + " FROM " + _keyValueList[0];
            _sqlstatement += " WHERE " + classkey + " = " + classId + " AND " + financekey + " = " + financeId;

            _dt = _db.GetDataTable(_sqlstatement);
        }
        catch (Exception Ex)
        {
        }

        return _dt;
    }

    public void clearProcess()
    {
        ddlCardclass.SelectedIndex = 0;
        lblMessage.Text = "";
        lblError.Text = "";
        imgCardLogo.ImageUrl = "~/captcha/temp_card.gif";
        lblServerUrl.Text = "";
        lblCCId.Text = "";
        ddlProductName.Items.Clear();
        ddlProductName.Items.Insert(0, new ListItem("Select", "-1"));

        DataTable _dtCompany = General.GetComboItem("ProviderCompany", false);
        ddlProviderCompanyName.DataSource = _dtCompany;
        ddlProviderCompanyName.DataTextField = _dtCompany.Columns[0].ColumnName.ToString();
        ddlProviderCompanyName.DataValueField = _dtCompany.Columns[1].ColumnName.ToString();
        ddlProviderCompanyName.DataBind();
        ddlProviderCompanyName.Items.Insert(0, new ListItem("Select", "-1"));
    }
}
